﻿<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 设备列表 -->
      <el-col :lg="24" :xs="24">
        <!-- 搜索区域 -->
        <div class="search-container">
          <el-form :inline="true" label-width="auto">
            <el-form-item label="设备类型">
              <el-select
                v-model="paramDeviceType"
                placeholder="全部"
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="item in deviceTypeAndModes"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="启用状态">
              <el-select v-model="paramIsEnable" placeholder="全部" clearable style="width: 120px">
                <el-option label="正常" :value="true" />
                <el-option label="禁用" :value="false" />
              </el-select>
            </el-form-item>

            <el-form-item class="search-buttons">
              <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
              <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="hover" class="data-table">
          <div class="data-table__toolbar">
            <div class="data-table__toolbar--actions">
              <el-button
                v-hasPerm="['sys:user:add']"
                type="success"
                icon="plus"
                @click="handleOpenDialog()"
              >
                新增
              </el-button>
            </div>
          </div>

          <!-- 设备列表 -->
          <el-table
            v-loading="loading"
            border
            stripe
            :data="tableData"
            highlight-current-row
            class="data-table__content"
          >
            <el-table-column label="序号" width="60px" type="index" align="center" />
            <el-table-column label="设备名称" align="center" prop="deviceName" />
            <el-table-column label="类型" align="center" prop="deviceType">
              <template #default="scope">
                {{ deviceTypeAndModes.find((a) => a.key == scope.row.deviceType)?.value }}
              </template>
            </el-table-column>
            <el-table-column label="型号" align="center" prop="deviceMode">
              <template #default="scope">
                {{
                  deviceTypeAndModes
                    .find((a) => a.key == scope.row.deviceType)
                    ?.children.find((a) => a.key == scope.row.deviceMode)?.value
                }}
              </template>
            </el-table-column>
            <el-table-column label="工位号" align="center" prop="stationNum" />
            <el-table-column label="状态" align="center" prop="isEnabled">
              <template #default="scope">
                <el-tag :type="scope.row.isEnabled ? 'success' : 'info'">
                  {{ scope.row.isEnabled ? "启用" : "禁用" }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="通讯类型" align="center">
              <template #default="scope">
                {{ scope.row.protocol == EnumProtocol.TCP ? "TCP" : "串口" }}
              </template>
            </el-table-column>
            <el-table-column label="TCP地址 | 串口号" align="center">
              <template #default="scope">
                {{
                  scope.row.protocol == EnumProtocol.TCP
                    ? `${scope.row.tcpIp}:${scope.row.tcpPort}`
                    : scope.row.comPort
                }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right">
              <template #default="scope">
                <el-button
                  v-hasPerm="'sys:user:edit'"
                  type="primary"
                  icon="edit"
                  link
                  size="small"
                  @click="handleOpenDialog(scope.row)"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  icon="delete"
                  link
                  size="small"
                  @click="handleDelete(scope.row.id)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 设备列表 end -->
        </el-card>
      </el-col>
    </el-row>

    <!-- 配置详情弹窗 -->
    <el-dialog
      v-if="dialogDetailVisible"
      v-model="dialogDetailVisible"
      width="700"
      :title="dialogTitle"
    >
      <DeviceDetail :form-data="dialogDetailData" @close="handleCloseDialog" />
    </el-dialog>
    <!-- 配置详情弹窗 End -->
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import deviceApi from "@/api/business/device.api";
import { EnumParity, EnumProtocol, EnumStopBits } from "@/enums";
import { useDevicesStore } from "@/store/business/devices.store";
import DeviceDetail from "@/views/business/devices/components/DeviceDetail.vue";
import { deepClone } from "@/utils";

const deviceStore = useDevicesStore();

const loading = ref(false);
// 查询参数：设备类型
const paramDeviceType = ref<string>();
// 查询参数：启用状态
const paramIsEnable = ref<boolean>();
// 表格数据
const tableData = ref<DeviceConfig[]>();
const deviceTypeAndModes = deviceStore.deviceTypeAndModes;
const dialogDetailVisible = ref(false);
const dialogDetailData = ref<DeviceConfig>();
const dialogTitle = ref("");

onMounted(() => {
  handleQuery();
});

// 查询方法
function handleQuery() {
  loading.value = true;
  deviceApi.getDevices(paramDeviceType.value, paramIsEnable.value).then((data) => {
    tableData.value = data;
  });
  loading.value = false;
}
function handleResetQuery() {
  paramDeviceType.value = undefined;
  paramIsEnable.value = undefined;
  handleQuery();
}
function handleOpenDialog(model?: DeviceConfig) {
  console.log("handleOpenDialog", model);

  dialogTitle.value = model ? "编辑设备" : "新增设备";
  const currentData = model
    ? deepClone(model)
    : {
        id: 0,
        deviceName: "",
        deviceType: "",
        deviceMode: "",
        stationNum: 1,
        isEnabled: true,
        protocol: EnumProtocol.TCP,
        tcpIp: "",
        tcpPort: 0,
        comPort: "",
        comBaudRate: 9600,
        comDataBits: 8,
        comStopBits: EnumStopBits.One,
        comParity: EnumParity.None,
      };
  dialogDetailData.value = { ...currentData };

  dialogDetailVisible.value = true;
}
function handleDelete(id: number) {
  ElMessageBox.confirm("确认删除该设备？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    deviceApi.delete(id).then(async () => {
      ElMessage.success("删除成功");
      handleQuery();
      deviceStore.loadActiveDevices();
    });
  });
}

function handleCloseDialog(refresh: boolean) {
  dialogDetailVisible.value = false;
  if (refresh) handleQuery();
}
</script>

<style scoped lang="scss"></style>
